package com.luoxiang.socialandroid.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.HomepageAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.fragment.JifentixianFragment;
import com.luoxiang.socialandroid.fragment.PointsDetailsFragment;
import com.luoxiang.socialandroid.fragment.RechargeRecordFragment;
import com.luoxiang.socialandroid.fragment.RecordsOfConsumptionFragment;
import com.luoxiang.socialandroid.fragment.WallettixianFragment;
import com.luoxiang.socialandroid.fragment.WithdrawalRecordFragment;
import com.luoxiang.socialandroid.viewpager.CustomViewPager;

import java.util.ArrayList;

public class MybillActivity extends BaseActivity {
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<Fragment> fragmentlist = new ArrayList<>();
    private TabLayout tab_wmybill;
    private CustomViewPager vp_mybill;
    private int ishost;
    private ImageView icon_back;

    @Override
    protected void initListener() {
        vp_mybill.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        list.clear();
        fragmentlist.clear();
//        if (ishost == 1) {
            list.add(getString(R.string.mi_chongzhijilu));
            list.add(getString(R.string.mi_xiaofeijilu));
            list.add(getString(R.string.mi_jifenmingxi));
            list.add(getString(R.string.mi_tixianjilu));
            fragmentlist.add(new RechargeRecordFragment());
            fragmentlist.add(new RecordsOfConsumptionFragment());
            fragmentlist.add(new WithdrawalRecordFragment());
            fragmentlist.add(new WallettixianFragment());
//        } else {
//            list.add(getString(R.string.mi_chongzhijilu));
//            list.add(getString(R.string.mi_xiaofeijilu));
//            list.add(getString(R.string.mi_jifenmingxi));
//            list.add(getString(R.string.mi_jifenduihuan));
//            fragmentlist.add(new RechargeRecordFragment());
//            fragmentlist.add(new RecordsOfConsumptionFragment());
//            fragmentlist.add(new WithdrawalRecordFragment());
//            fragmentlist.add(new PointsDetailsFragment());
//        }


        HomepageAdapter pagerAdapter = new HomepageAdapter(getSupportFragmentManager(), fragmentlist, list);
        vp_mybill.setAdapter(pagerAdapter);
        tab_wmybill.setupWithViewPager(vp_mybill);
        setTabStyle();
        tab_wmybill.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(Color.parseColor("#835DFF"));
                tv.setTextSize(16);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(Color.parseColor("#333333"));
                tv.setTextSize(16);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @SuppressLint({"WrongConstant", "ResourceType"})
    private void setTabStyle() {
        //根据Tab数量循环来设置
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = this.tab_wmybill.getTabAt(i);
            if (tab != null) {
                View view = LayoutInflater.from(MybillActivity.this).inflate(R.layout.text_tablayout, null);
                //设置Tab标题
                ((TextView) view.findViewById(R.id.tab_tv)).setText(list.get(i));
                //第一个默认为选择样式
                if (i == 0) {
                    ((TextView) view.findViewById(R.id.tab_tv)).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    ((TextView) view.findViewById(R.id.tab_tv)).setTextColor(Color.parseColor("#835DFF"));
                    ((TextView) view.findViewById(R.id.tab_tv)).setTextSize(16);
                } else {
                }
                //最后添加view到Tab上面
                tab.setCustomView(view);
            }
        }
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_mybill);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        tab_wmybill = findViewById(R.id.tab_wmybill);
        vp_mybill = findViewById(R.id.vp_mybill);
        Intent intent = getIntent();
        ishost = intent.getIntExtra("ishost", -1);
        icon_back = findViewById(R.id.icon_back);
    }
}